import {getBosAk} from './base.js'
import {BosClient} from 'bce-sdk-js';
export function generateUUID() {
  let d = new Date().getTime();
  let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
      let r = (d + Math.random() * 16) % 16 | 0;
      d = Math.floor(d / 16);
      return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  });
  return uuid;
};
export function mimed(ext) {
  let mime = {
      txt: 'text/plain',
      doc: 'application/msword',
      exe: 'application/octet-stream',
      pdf: 'application/pdf',
      ps: 'application/postscript',
      xlm: 'application/vnd.ms-excel',
      xls: 'application/vnd.ms-excel',
      ppt: 'application/vnd.ms-powerpoint',
      tgz: 'application/x-compressed',
      gz: 'application/x-gzip',
      mvb: 'application/x-msmediaview',
      swf: 'application/x-shockwave-flash',
      tar: 'application/x-tar',
      zip: 'application/zip',
      mp3: 'audio/mpeg',
      wav: 'audio/x-wav',
      bmp: 'image/bmp',
      gif: 'image/gif',
      jpe: 'image/jpeg',
      jpeg: 'image/jpeg',
      png: 'image/png',
      jpg: 'image/jpeg',
      mp2: 'video/mpeg',
      mpa: 'video/mpeg',
      mpe: 'video/mpeg',
      mpeg: 'video/mpeg',
      mpg: 'video/mpeg',
      mpv2: 'video/mpeg',
      mov: 'video/quicktime',
      qt: 'video/quicktime',
      avi: 'video/x-msvideo'
  };
  return mime[ext];
};

function dataURItoBlob(base64Data) {
  console.log(base64Data);//data:image/png;base64,
  var byteString;
  if(base64Data.split(',')[0].indexOf('base64') >= 0)
      byteString = atob(base64Data.split(',')[1]);//base64 解码
  else{
      byteString = unescape(base64Data.split(',')[1]);
  }
  var mimeString = base64Data.split(',')[0].split(':')[1].split(';')[0];//mime类型 -- image/png

  // var arrayBuffer = new ArrayBuffer(byteString.length); //创建缓冲数组
  // var ia = new Uint8Array(arrayBuffer);//创建视图
  var ia = new Uint8Array(byteString.length);//创建视图
  for(var i = 0; i < byteString.length; i++) {
      ia[i] = byteString.charCodeAt(i);
  }
  var blob = new Blob([ia], {
      type: mimeString
  });
  return blob;
}
export const uploadImage = (file) => {
  let tempFilePaths = file.path
  
  const _this = this
  let bosAkInfo = getBosAk()
  bosAkInfo.then(res => {
    let config = {
      endpoint: 'https://sts.bj.baidubce.com',
      credentials: {
        ak: res.accessKeyId,
        sk: res.secretAccessKey,
      },
      sessionToken: res.sessionToken
    }; 
    let ext = tempFilePaths.split(/\./g).pop(); 
    let myDate = new Date();
    let time = myDate.toJSON().split('T').join('').substr(0, 10).replace(/-/g, '');
    let filePath = `zjz/${time}/${generateUUID()}.${ext}`;

      let options = {
        'content-length': String(file.size), // 添加http header
        'Content-Type': mimed(ext),
        'x-bce-object-type': mimed(ext)
      };
      let bucket = 'txy-dev';
      let client = new BosClient(config);
      client.putObjectFromFile(bucket, filePath, tempFilePaths, options).then(res => {
        console.log(res)
        // resolve(res);
      });
      // uni.getFileSystemManager().readFile({
      //   filePath: tempFilePaths, //选择图片返回的相对路径
      //   encoding: 'base64', //编码格式
      //   success: res => {
      //       // 成功的回调
      //       // 'data:image/jpeg;base64,'
      //       // let fileblob = dataURItoBlob('data:image/jpeg;base64,' + res.data)
      //       client.putObjectFromFile(bucket, filePath, res.data, options).then(res => {
      //         console.log(res)
      //         // resolve(res);
      //       });
      //   }
      // });
      
      // client.putObjectFromFile(bucket, filePath, tempFilePaths, options).then(res => {
      //   console.log(res)
      //   // resolve(res);
      // });
    // readFile(file).then(list => {
    //   let data = list.Int8Array;
    //   let fileSize = list.byteLength;
    //   console.log(fileSize)
    //   let options = {
    //     'content-length': String(fileSize), // 添加http header
    //     'Content-Type': mimed(ext),
    //     'x-bce-object-type': mimed(ext)
    //   };
    //   let bucket = 'txy-dev';
    //   let client = new BosClient(config);
    //   client.putObject(bucket, filePath, list, options).then(res => {
    //     console.log(res)
    //     // resolve(res);
    //   });
    // })
  })
}

export const readFile = (file) => {
  return new Promise(resolve => {
    let fm = wx.getFileSystemManager()
    fm.readFile({
      filePath: file,
      success: function (res) {
        console.log(res.data)
        resolve(res.data)
      }
    })
  })
}